/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HelpCnt.java
 *
 * Created on 2012-01-24, 14:14:51
 */

package Visual.Help;

import Tools.CharsetControl;
import Tools.ConfigurationManager;
import Visual.Tools.InternationalizedTabbedPane;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

/**
 *
 * @author pawelkn
 */
public class HelpCtx extends javax.swing.JDialog {

    private Stack reverseStack = new Stack();
    private Stack forwardStack = new Stack();
    private HelpTree helpTree;
    
    private boolean clearForwardStack = true;

    private static ResourceBundle bundle;

    /** Creates new form HelpCnt */
    public HelpCtx(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        Locale locale = new Locale(ConfigurationManager.load().getLocale());
        ResourceBundle.clearCache();
        bundle = ResourceBundle.getBundle("Bundle", locale, new CharsetControl());

        initComponents();

        helpTree = new HelpTree(this);
        jPanel2.add(helpTree);
        setPageNode(helpTree.findHelpNode("index.html"));

        htmlPane.addHyperlinkListener( new HyperlinkListener(){
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    HelpNode node = helpTree.findHelpNode(e.getURL().toString());
                    if( node != null ) {
                        helpTree.setSelectedNode(node);
                    }
                }
            }
        });
        
        HTMLDocument doc = (HTMLDocument)htmlPane.getDocument();
        File currentDir = new File( System.getProperty( "user.dir" ) + "/help/" + locale.getLanguage());
        try {
            doc.setBase(currentDir.toURI().toURL());
        } catch (MalformedURLException ex) {
            Logger.getLogger("USBThermometer").log(Level.ALL, null, ex);
        }

        Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
    }

    public void setPageNode(HelpNode node) {
        try {
            htmlPane.setPage(node.getContentURL());
            reverseStack.push(node);
            
            if( clearForwardStack ) {
                forwardStack.clear();
            } 
            clearForwardStack = true;            
        } catch (IOException ex) {
            Logger.getLogger("USBThermometer").log(Level.ALL, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        htmlPane = new javax.swing.JEditorPane();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new InternationalizedTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jToolBar2 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setTitle(bundle.getString("USB_THERMOMETER_HELP")); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jSplitPane1.setDividerLocation(200);

        htmlPane.setEditable(false);
        htmlPane.setEditorKit(new HTMLEditorKit());
        htmlPane.setPreferredSize(new java.awt.Dimension(1000, 600));
        htmlPane.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                htmlPaneKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(htmlPane);

        jSplitPane1.setRightComponent(jScrollPane1);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));
        jTabbedPane1.addTab("CONTENTS", jPanel2);

        jPanel1.add(jTabbedPane1);

        jSplitPane1.setLeftComponent(jPanel1);

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);

        jToolBar2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar2.setFloatable(false);

        jButton1.setIcon(new ImageIcon("graphics/arrow-180.png"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton1);

        jButton2.setIcon(new ImageIcon("graphics/arrow.png"));
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1112, Short.MAX_VALUE)
            .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
    }//GEN-LAST:event_closeDialog

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (reverseStack.size() <= 1) {
            return;
        }
        forwardStack.push( reverseStack.peek() );
        clearForwardStack = false;
        
        reverseStack.pop();
        HelpNode node = (HelpNode) reverseStack.peek();
        reverseStack.pop();
        if( node != null ) {
            helpTree.setSelectedNode(node);
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void htmlPaneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_htmlPaneKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_F5) {
            Document doc = htmlPane.getDocument();
            doc.putProperty(Document.StreamDescriptionProperty, null);
            try {
                HelpNode node = helpTree.getSelectedNode();
                htmlPane.setPage(node.getContentURL());
            } catch (IOException ex) {
                Logger.getLogger("USBThermometer").log(Level.ALL, null, ex);
            }
        }
    }//GEN-LAST:event_htmlPaneKeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (forwardStack.size() <= 0) {
            return;
        }
        clearForwardStack = false;
        
        HelpNode node = (HelpNode) forwardStack.peek();
        forwardStack.pop();
        if( node != null ) {
            helpTree.setSelectedNode(node);
        }
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane htmlPane;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    // End of variables declaration//GEN-END:variables

}
